package com.jp.knowledge.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.broad.LocalBroadCast;
import com.jp.knowledge.util.SharedUtil;

import org.xutils.common.util.DensityUtil;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;


/**
 * Created by jiayen on 16/12/15.
 * <p>
 * 设置字体大小dialog
 */

public class SetFontDialog extends Dialog implements View.OnClickListener {


    private Context mContext;
    private Window window;

    private View rootView;

    @ViewInject(R.id.min_font)
    private TextView minFont;

    @ViewInject(R.id.middle_font)
    private TextView midleFont;

    @ViewInject(R.id.big_font)
    private TextView bigFont;

    @ViewInject(R.id.cancle_setfont_btn)
    private TextView cancleBtn;

    private TextView  mFontSizeTv;

    private LocalBroadcastManager localManager;

    private CustomerFontSetCallBack   fontCallBack;
    private boolean customer;
    public SetFontDialog(Context context,TextView mFontSizeTv) {
        this(context, 0);
        this.mFontSizeTv = mFontSizeTv;
    }

    public void setCustomer(boolean customer) {
        this.customer = customer;
    }

    public void setFontCallBack(CustomerFontSetCallBack fontCallBack) {
        this.fontCallBack = fontCallBack;
    }

    public SetFontDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.mContext=context;
        initView();
    }



    /**
     * 初始化view
     */
    private void initView() {
        localManager = LocalBroadcastManager.getInstance(mContext);

        rootView = getLayoutInflater().inflate(R.layout.set_font_dialog, null);
        x.view().inject(this,rootView);
        minFont.setOnClickListener(this);
        midleFont.setOnClickListener(this);
        bigFont.setOnClickListener(this);
        cancleBtn.setOnClickListener(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(rootView);
        window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);

        window.setBackgroundDrawableResource(R.color.translate); //设置对话框背景为透明
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.width= DensityUtil.getScreenWidth();
        window.setAttributes(wl);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.min_font:
                setValue(14,"小");
                break;
            case R.id.middle_font:

                setValue(16,"中");
                break;
            case R.id.big_font:
                setValue(20,"大");
                break;
            case R.id.cancle_setfont_btn:
               this.dismiss();
                break;
        }
    }

    private void setValue(int size ,String text){
        if(customer){
            if(fontCallBack!=null)fontCallBack.customerSetFont(size);
        }
            SharedUtil.getInstance(mContext).saveCommFontSize(size);
            SharedUtil.getInstance(mContext).saveCommFontSize(text);
            if(mFontSizeTv!=null)
            mFontSizeTv.setText(text);

            //发送广播改变字体
            Intent intent = new Intent(LocalBroadCast.COMM_CHANGE_SIZE);
            localManager.sendBroadcast(intent);

        this.dismiss();
    }

    public interface    CustomerFontSetCallBack{
        void  customerSetFont(int size);
    }
}
